package secrpc

import (
	"dub/define/grpcmodel"
	"dub/utils"
	"golang.org/x/net/context"
)

type SecRpcAdvert struct {
	log   *utils.Logger
	dbRpc *utils.RpcProxy
}

func (s *SecRpcAdvert) Find(ctx context.Context, args *grpcmodel.ModelSecRpcAdvertReqFind) (reply *grpcmodel.ModelSecRpcAdvertResFind, err error) {
	reply = &grpcmodel.ModelSecRpcAdvertResFind{}
	for {
		db_args := &grpcmodel.ModelDbRpcAdvertReqFind{
			StartRow: args.GetStartRow(),
			PageSize: args.GetPageSize(),
		}
		db_Advert := grpcmodel.NewIDbRpcAdvertClient(s.dbRpc.Conn())
		s.log.Infof("def_func_sec_advert.go Find method call db_Advert.Find db_args info is %+v\n", *db_args)
		db_reply, err := db_Advert.Find(s.dbRpc.Context(), db_args)
		s.log.Infof("def_func_sec_advert.go Find method call db_advert.Find db_reply info is %+v\n", *db_reply)
		if err != nil {
			s.log.Errorf("def_func_sec_advert.go Find method call db_advert.Find err. %v\n", err)
			reply.Err = 2
			break
		}
		if db_reply.GetErr() > 0 {
			if db_reply.GetErr() > 1 {
				s.log.Errorf("def_func_sec_advert.go Find method call db_advert.Find err. %v\n", err)
			} else {
				s.log.Errorf("def_func_sec_advert.go Find method call db_advert.Find no find info. %v\n", err)
			}
			reply.Err = db_reply.GetErr()
			break
		}

		reply.Total = db_reply.GetTotal()
		res_models := make([]*grpcmodel.ModelSecRpcAdvert, 0, len(db_reply.GetRows()))
		for _, advert := range db_reply.GetRows() {
			model := &grpcmodel.ModelSecRpcAdvert{
				AdvertId:  advert.GetAdvertId(),
				AdTitle:   advert.GetAdTitle(),
				AdUrl:     advert.GetAdUrl(),
				AdType:    advert.GetAdType(),
				AdCSPrice: advert.GetAdCSPrice(),
				AdCount:   advert.GetAdCount(),
				AdStatus:  advert.GetAdStatus(),
				AdTCount:  advert.GetAdTCount(),
			}
			res_models = append(res_models, model)
		}
		reply.Rows = res_models
		break
	}
	return
}

func (s *SecRpcAdvert) Update(ctx context.Context, args *grpcmodel.ModelSecRpcAdvertReqModel) (reply *grpcmodel.ModelSecRpcAdvertResModel, err error) {
	reply = &grpcmodel.ModelSecRpcAdvertResModel{}
	for {
		arg_model := &grpcmodel.ModelDbRpcAdvert{
			AdvertId:  args.GetModelSecRpcAdvert().GetAdvertId(),
			AdTitle:   args.GetModelSecRpcAdvert().GetAdTitle(),
			AdUrl:     args.GetModelSecRpcAdvert().GetAdUrl(),
			AdType:    args.GetModelSecRpcAdvert().GetAdType(),
			AdCSPrice: args.GetModelSecRpcAdvert().GetAdCSPrice(),
			AdCount:   args.GetModelSecRpcAdvert().GetAdCount(),
			AdTCount:  args.GetModelSecRpcAdvert().GetAdTCount(),
			AdStatus:  args.GetModelSecRpcAdvert().GetAdStatus(),
		}
		up_model := &grpcmodel.ModelDbRpcAdvert{
			AdvertId: arg_model.GetAdvertId(),
		}
		db_advert := grpcmodel.NewIDbRpcAdvertClient(s.dbRpc.Conn())
		if args.GetIsAllUpdate() {
			//把要修改的信息查询出来
			f_args := &grpcmodel.ModelDbRpcAdvertReqFind{AdvertId: args.GetModelSecRpcAdvert().GetAdvertId()}
			s.log.Infof("def_func_sec_advert.go Update method call db_advert.FindAdvert f_args info is %+v\n", f_args)
			f_reply, err := db_advert.FindAdvert(s.dbRpc.Context(), f_args)
			s.log.Infof("def_func_sec_advert.go Update method call db_advert.FindAdvert r_reply info is %+v\n", f_reply)
			if err != nil {
				s.log.Errorf("def_func_sec_advert.go Update method call db_advert.FindAdvert err. %v\n", err)
				reply.Err = 2
				break
			}
			if f_reply.GetErr() > 0 {
				if f_reply.GetErr() > 1 {
					s.log.Errorf("def_func_sec_advert.go Update method call db_advert.FindAdvert err. %v\n", err)
				} else {
					s.log.Errorf("def_func_sec_advert.go Update method no find error err. %v\n", err)
				}
				reply.Err = f_reply.GetErr()
				break
			}
			up_model = f_reply.GetModelDbRpcAdvert()
		}

		if args.GetIsAllUpdate() || (!args.GetIsAllUpdate() && arg_model.GetAdCSPrice() > 0) {
			up_model.AdCSPrice = arg_model.GetAdCSPrice()
		}
		if args.GetIsAllUpdate() || (!args.GetIsAllUpdate() && arg_model.GetAdCount() > 0) {
			up_model.AdCount = arg_model.GetAdCount()
		}
		if args.GetIsAllUpdate() || (!args.GetIsAllUpdate() && arg_model.GetAdStatus() > 0) {
			up_model.AdStatus = arg_model.GetAdStatus()
		}
		if args.GetIsAllUpdate() || (!args.GetIsAllUpdate() && arg_model.GetAdTCount() > 0) {
			up_model.AdTCount = arg_model.GetAdTCount()
		}
		if args.GetIsAllUpdate() || (!args.GetIsAllUpdate() && arg_model.GetAdTitle() != "") {
			up_model.AdTitle = arg_model.GetAdTitle()
		}
		if args.GetIsAllUpdate() || (!args.GetIsAllUpdate() && arg_model.GetAdType() > 0) {
			up_model.AdType = arg_model.GetAdType()
		}
		if args.GetIsAllUpdate() || (!args.GetIsAllUpdate() && arg_model.GetAdUrl() != "") {
			up_model.AdUrl = arg_model.GetAdUrl()
		}

		u_args := &grpcmodel.ModelDbRpcAdvertReqModel{
			IsAllUpdate:      args.GetIsAllUpdate(),
			ModelDbRpcAdvert: up_model,
		}
		s.log.Infof("def_func_sec_advert.go Update method call db_advert.Update u_args info is %+v\n", u_args)
		u_reply, err := db_advert.Update(s.dbRpc.Context(), u_args)
		s.log.Infof("def_func_sec_advert.go Update method call db_advert.Update u_reply info is %+v\n", u_reply)
		if err != nil {
			s.log.Errorf("def_func_sec_advert.go Update method call db_advert.Update err. %v \n", err)
			reply.Err = 2
			break
		}
		if u_reply.GetErr() > 0 {
			if u_reply.GetErr() > 1 {
				s.log.Errorf("def_func_sec_advert.go Update method call db_advert.Update err. %v\n", err)
			} else {
				s.log.Errorf("def_func_sec_advert.go Update method no find error err. %v\n", err)
			}
			reply.Err = u_reply.GetErr()
			break
		}
		reply.AdvertId = arg_model.GetAdvertId()
		break
	}
	return
}

func (s *SecRpcAdvert) FindAdvert(ctx context.Context, args *grpcmodel.ModelSecRpcAdvertReqFind) (reply *grpcmodel.ModelSecRpcAdvertResFindAdvert, err error) {
	reply = &grpcmodel.ModelSecRpcAdvertResFindAdvert{}
	for {
		db_args := &grpcmodel.ModelDbRpcAdvertReqFind{
			AdvertId: args.GetAdvertId(),
		}
		db_advert := grpcmodel.NewIDbRpcAdvertClient(s.dbRpc.Conn())
		s.log.Infof("def_func_sec_advert.go FindAdvert method db_advert.FindAdvert db_args info is %+v\n", db_args)
		db_reply, err := db_advert.FindAdvert(s.dbRpc.Context(), db_args)
		s.log.Infof("def_func_sec_advert.go FindAdvert method db_advert.FindAdvert db_reply info is %+v\n", db_reply)
		if err != nil {
			s.log.Errorf("def_func_sec_advert.go FindAdvert method call db_advert.FindAdvert err. %v \n", err)
			reply.Err = 2
			break
		}
		if db_reply.GetErr() > 0 {
			if db_reply.GetErr() > 1 {
				s.log.Errorf("def_func_sec_advert.go FindAdvert method call db_advert.FindAdvert err. %v\n", err)
			} else {
				s.log.Errorf("def_func_sec_advert.go FindAdvert method no find error err. %v\n", err)
			}
			reply.Err = db_reply.GetErr()
			break
		}

		db_model := db_reply.GetModelDbRpcAdvert()
		re_model := &grpcmodel.ModelSecRpcAdvert{
			AdvertId:  db_model.GetAdvertId(),
			AdTitle:   db_model.GetAdTitle(),
			AdUrl:     db_model.GetAdUrl(),
			AdType:    db_model.GetAdType(),
			AdCSPrice: db_model.GetAdCSPrice(),
			AdCount:   db_model.GetAdCount(),
			AdStatus:  db_model.GetAdStatus(),
			AdTCount:  db_model.GetAdTCount(),
		}
		reply.ModelSecRpcAdvert = re_model
		break
	}
	return
}

func (s *SecRpcAdvert) Add(ctx context.Context, args *grpcmodel.ModelSecRpcAdvertReqModel) (reply *grpcmodel.ModelSecRpcAdvertResModel, err error) {
	reply = &grpcmodel.ModelSecRpcAdvertResModel{}
	for {
		db_args := &grpcmodel.ModelDbRpcAdvertReqModel{
			IsAllUpdate: args.GetIsAllUpdate(),
			ModelDbRpcAdvert: &grpcmodel.ModelDbRpcAdvert{
				AdvertId:  args.GetModelSecRpcAdvert().GetAdvertId(),
				AdTitle:   args.GetModelSecRpcAdvert().GetAdTitle(),
				AdUrl:     args.GetModelSecRpcAdvert().GetAdUrl(),
				AdType:    args.GetModelSecRpcAdvert().GetAdType(),
				AdCSPrice: args.GetModelSecRpcAdvert().GetAdCSPrice(),
				AdCount:   args.GetModelSecRpcAdvert().GetAdCount(),
				AdTCount:  args.GetModelSecRpcAdvert().GetAdTCount(),
				AdStatus:  args.GetModelSecRpcAdvert().GetAdStatus(),
			},
		}

		db_advert := grpcmodel.NewIDbRpcAdvertClient(s.dbRpc.Conn())
		s.log.Infof("def_func_sec_advert.go Add method db_advert.Add db_args info is %+v\n", db_args)
		db_reply, err := db_advert.Add(s.dbRpc.Context(), db_args)
		s.log.Infof("def_func_sec_advert.go Add method db_advert.Add db_reply info is %+v\n", db_reply)
		if err != nil {
			s.log.Errorf("def_func_sec_advert.go Add method call db_advert.Add err. %v \n", err)
			reply.Err = 2
			break
		}
		if db_reply.GetErr() > 0 {
			if db_reply.GetErr() > 1 {
				s.log.Errorf("def_func_sec_advert.go Add method call db_advert.Add err. %v\n", err)
			} else {
				s.log.Errorf("def_func_sec_advert.go Add method no find error err. %v\n", err)
			}
			reply.Err = db_reply.GetErr()
			break
		}

		reply.AdvertId = db_reply.GetAdvertId()
		break
	}
	return
}

var _secRpcAdvert *SecRpcAdvert

func NewSecRpcAdvert(log *utils.Logger, dbRpc *utils.RpcProxy) *SecRpcAdvert {
	if _secRpcAdvert == nil {
		_secRpcAdvert = &SecRpcAdvert{
			log:   log,
			dbRpc: dbRpc,
		}
	}
	return _secRpcAdvert
}
